package org.example.controller;

import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import io.github.resilience4j.ratelimiter.annotation.RateLimiter;
import io.github.resilience4j.retry.annotation.Retry;
import jakarta.annotation.Resource;
import org.example.entity.Business;
import org.example.entity.CommonResult;
import org.example.entity.Food;
import org.example.feign.FoodServerService;
import org.example.service.BusinessService;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RefreshScope
public class BusinessController {
    @Resource
    private BusinessService businessService;
    @Resource
    private FoodServerService foodServerService;
    //原接口:BusinessController/listBusinessByOrderTypeId
    @GetMapping("/BusinessController/listAllBusinessByOrderTypeId/{orderTypeId}")
    public CommonResult<List> listAllBusinessByOrderTypeId(@PathVariable("orderTypeId") Integer orderTypeId){
        List<Business> businessList = businessService.findAllBusinessByType(orderTypeId);
        if(businessList != null){
            return new CommonResult<>(200, "找到orderTypeId为"+orderTypeId+"的商家", businessList);
        }else{
            return new CommonResult<>(404, "找不到该"+orderTypeId+"的商家", null);
        }
    }
    @GetMapping("/BusinessController/getBusinessFoodByBusinessId/{id}")
    @Retry(name="retry1", fallbackMethod = "findAllFoodsByBusinessidDownRetry")
    @RateLimiter(name="rate1", fallbackMethod = "findAllFoodsByBusinessidDownRate")
    public CommonResult<Business> getBusinessFoodByBusinessId(@PathVariable("id") Integer id){
        Business business = businessService.findBusinessById(String.valueOf(id));
        CommonResult<List> cr = foodServerService.findAllByBusinessid(id);
        List foods = cr.getResult();
        String msg = cr.getMessage();
        business.setFoodList(foods);
        if(foods != null){
            System.out.println(msg);
            return new CommonResult<>(200, msg+":找到BusinessID为"+id+"的食品列表，返回对应business", business);

        }else{
            return new CommonResult<>(404, msg+":找不到BusinessID为"+id+"的食品列表", null);
        }

    }
    public CommonResult<Business> findAllFoodsByBusinessidDownRetry(Integer id,Throwable e){ //这个callback方法接受的参数首先需要有原方法 接收的参数. 同时还有一个异常 参数
        //可以根据产生的异常不同，可以设置不同的异常类型，Throwable是最广泛的，比如：SQLDataException（这个异常对应的返回的异常信息就是 ：请联系管理员，当前数据库异常
//        e.printStackTrace();
        String message = "获取"+id+"信息的服务当前被熔断，因此方法降级Retry";
        System.out.println(message);
        CommonResult<Business> result = new CommonResult<>(555, message, null);
        return result;
    }
    public CommonResult<Business> findAllFoodsByBusinessidDownRate(Integer id,Throwable e){ //这个callback方法接受的参数首先需要有原方法 接收的参数. 同时还有一个异常 参数
        //可以根据产生的异常不同，可以设置不同的异常类型，Throwable是最广泛的，比如：SQLDataException（这个异常对应的返回的异常信息就是 ：请联系管理员，当前数据库异常
//        e.printStackTrace();
        String message = "获取"+id+"信息的服务当前被熔断，因此方法降级Rate";
        System.out.println(message);
        CommonResult<Business> result = new CommonResult<>(555, message, null);
        return result;
    }
}
